<template>
  <div class="exception-notice">
    <el-card class="exception-card">
      <template #header>
        <div class="card-header">
          <span>异常考勤通知</span>
          <el-button type="primary" @click="fetchExceptionList" size="small">
            <el-icon><Refresh /></el-icon>刷新数据
          </el-button>
        </div>
      </template>

      <!-- 异常记录列表 -->
      <el-table 
        :data="exceptionList" 
        border 
        style="width: 100%" 
        v-loading="loading"
        class="exception-table"
        :header-cell-style="{
          background: '#f8faff',
          color: '#303133',
          fontWeight: '600',
          fontSize: '14px'
        }"
      >
        <el-table-column prop="date" label="日期" width="120" />
        <el-table-column prop="checkType" label="异常类型" width="120">
          <template #default="scope">
            <el-tag :type="getExceptionType(scope.row.checkTypeDesc)" effect="light">
              {{ scope.row.checkTypeDesc }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="reason" label="异常原因" />
        <el-table-column prop="status" label="是否提交申诉" width="120">
          <template #default="scope">
            <el-tag :type="scope.row.status === 1 ? 'success' : 'warning'">
              {{ scope.row.status === 1 ? '已提交' : '未提交' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="submittedToAdmin" label="处理状态" width="120">
          <template #default="scope">
            <el-tag :type="scope.row.submittedToAdmin === 1 ? 'success' : 'info'">
              {{ scope.row.submittedToAdmin === 1 ? '已处理' : '未处理' }}
            </el-tag>
          </template>
        </el-table-column>
        <el-table-column prop="remark" label="处理理由"> </el-table-column>
        <el-table-column label="操作" width="150" fixed="right">
          <template #default="scope">
            <el-button
              type="primary"
              size="small"
              @click="handleContact(scope.row)"
              v-if="scope.row.status === 0"
            >
              <el-icon><Message /></el-icon>联系管理员
            </el-button>
          </template>
        </el-table-column>
      </el-table>

      <!-- 分页 -->
      <div class="pagination-container">
        <el-pagination
          v-model:current-page="currentPage"
          v-model:page-size="pageSize"
          :page-sizes="[10, 20, 30, 50]"
          layout="total, sizes, prev, pager, next, jumper"
          :total="total"
          @size-change="handleSizeChange"
          @current-change="handleCurrentChange"
          background
        />
      </div>
    </el-card>

    <!-- 联系管理员对话框 -->
    <el-dialog v-model="contactDialogVisible" title="联系管理员" width="500px">
      <el-form ref="contactFormRef" :model="contactForm" :rules="contactRules" label-width="100px">
        <el-form-item label="异常说明" prop="explanation">
          <el-input
            v-model="contactForm.explanation"
            type="textarea"
            rows="3"
            placeholder="请详细说明异常情况"
          >
            <template #prefix>
              <el-icon><Document /></el-icon>
            </template>
          </el-input>
        </el-form-item>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="contactDialogVisible = false">
            <el-icon><Close /></el-icon>取消
          </el-button>
          <el-button type="primary" @click="submitContact()">
            <el-icon><Check /></el-icon>提交
          </el-button>
        </span>
      </template>
    </el-dialog>
  </div>
</template>

<script setup>
import { ref, reactive, onMounted } from 'vue'
import { ElMessage } from 'element-plus'
import { getExceptionList, submitAppeal } from '@/api/employee/exception'
import { Message, Document, Check, Close, Refresh } from '@element-plus/icons-vue'

// 加载状态
const loading = ref(false)

// 异常记录数据
const exceptionList = ref([])

// 分页相关
const currentPage = ref(1)
const pageSize = ref(10)
const total = ref(0)

// 联系管理员对话框
const contactDialogVisible = ref(false)
const contactFormRef = ref(null)
const contactForm = reactive({
  explanation: '',
  files: [],
  recordId: null, // 当前处理的异常记录ID
})

// 详情对话框
const detailDialogVisible = ref(false)
const currentDetail = ref({})

// 表单验证规则
const contactRules = {
  explanation: [
    { required: true, message: '请输入异常说明', trigger: 'blur' },
    { min: 10, max: 500, message: '长度在 10 到 500 个字符', trigger: 'blur' },
  ],
}

// 获取异常类型对应的标签类型
const getExceptionType = (type) => {
  const typeMap = {
    迟到: 'warning',
    早退: 'warning',
    缺勤: 'danger',
    打卡异常: 'info',
  }
  return typeMap[type] || 'info'
}

// 获取异常记录列表
const fetchExceptionList = async () => {
  loading.value = true
  try {
    const employeeNo = localStorage.getItem('employeeNo')
    if (!employeeNo) {
      ElMessage.error('获取员工编号失败')
      return
    }
    const res = await getExceptionList(employeeNo, currentPage.value, pageSize.value)
    if (res.data) {
      exceptionList.value = res.data.records
      total.value = res.data.total
    }
  } catch (error) {
    console.error('获取异常记录失败：', error)
    ElMessage.error('获取异常记录失败')
  } finally {
    loading.value = false
  }
}

// 联系管理员
const handleContact = (row) => {
  contactForm.explanation = `关于${row.date}的${row.checkTypeDesc}异常说明：`
  contactForm.recordId = row.id
  contactDialogVisible.value = true
}

// 提交联系表单
const submitContact = async () => {
  if (!contactFormRef.value) return

  try {
    await contactFormRef.value.validate(async (valid) => {
      if (valid) {
        const employeeNo = localStorage.getItem('employeeNo')
        if (!employeeNo) {
          ElMessage.error('获取员工编号失败')
          return
        }

        const data = {
          employeeNo: employeeNo,
          recordId: contactForm.recordId,
          explanation: contactForm.explanation,
        }

        const res = await submitAppeal(data)
        if (res.data) {
          ElMessage.success('提交成功，请等待管理员处理')
          contactDialogVisible.value = false
          // 重置表单
          contactForm.explanation = ''
          contactForm.recordId = null
          // 刷新列表
          fetchExceptionList()
        }
      }
    })
  } catch (error) {
    console.error('提交申诉失败：', error)
    ElMessage.error('提交失败')
  }
}

// 分页处理
const handleSizeChange = (val) => {
  pageSize.value = val
  fetchExceptionList()
}

const handleCurrentChange = (val) => {
  currentPage.value = val
  fetchExceptionList()
}

// 页面加载时获取数据
onMounted(() => {
  fetchExceptionList()
})
</script>

<style scoped lang="scss">
.exception-notice {
  max-width: 1200px;
  margin: 0 auto;
  padding: 20px;
  
  .exception-card {
    border-radius: 12px;
    box-shadow: 0 8px 24px rgba(0, 0, 0, 0.05);
    transition: all 0.3s ease;
    overflow: hidden;
    
    &:hover {
      box-shadow: 0 12px 32px rgba(24, 144, 255, 0.1);
      transform: translateY(-2px);
    }
  }
  
  .card-header {
    display: flex;
    justify-content: space-between;
    align-items: center;
    padding: 10px 0;
    
    span {
      font-size: 18px;
      font-weight: 600;
      color: #303133;
      position: relative;
      padding-left: 12px;
      
      &::before {
        content: '';
        position: absolute;
        left: 0;
        top: 50%;
        transform: translateY(-50%);
        width: 4px;
        height: 18px;
        background: #1890ff;
        border-radius: 2px;
      }
    }
  }
  
  .exception-table {
    margin-top: 16px;
    border-radius: 8px;
    overflow: hidden;
    box-shadow: 0 2px 12px rgba(0, 0, 0, 0.04);
    
    :deep(.el-table__row) {
      transition: all 0.3s;
      
      &:hover {
        background-color: #f8faff !important;
      }
    }
    
    :deep(.el-table__cell) {
      padding: 10px 0;
    }
  }
  
  :deep(.el-tag) {
    padding: 0 10px;
    height: 28px;
    line-height: 28px;
    border-radius: 4px;
    font-weight: 500;
    font-size: 12px;
    
    &.el-tag--success {
      background-color: rgba(82, 196, 26, 0.1);
      border-color: rgba(82, 196, 26, 0.2);
      color: #52c41a;
      
      &.el-tag--dark {
        background-color: #52c41a;
        border-color: #52c41a;
        color: #fff;
      }
      
      &.el-tag--light {
        background-color: rgba(82, 196, 26, 0.08);
        border-color: rgba(82, 196, 26, 0.3);
        color: rgba(82, 196, 26, 0.85);
      }
    }
    
    &.el-tag--info {
      background-color: rgba(144, 147, 153, 0.1);
      border-color: rgba(144, 147, 153, 0.2);
      color: #909399;
      
      &.el-tag--dark {
        background-color: #909399;
        border-color: #909399;
        color: #fff;
      }
      
      &.el-tag--light {
        background-color: rgba(144, 147, 153, 0.08);
        border-color: rgba(144, 147, 153, 0.3);
        color: rgba(144, 147, 153, 0.85);
      }
    }
    
    &.el-tag--warning {
      background-color: rgba(250, 173, 20, 0.1);
      border-color: rgba(250, 173, 20, 0.2);
      color: #faad14;
      
      &.el-tag--dark {
        background-color: #faad14;
        border-color: #faad14;
        color: #fff;
      }
      
      &.el-tag--light {
        background-color: rgba(250, 173, 20, 0.08);
        border-color: rgba(250, 173, 20, 0.3);
        color: rgba(250, 173, 20, 0.85);
      }
    }
    
    &.el-tag--danger {
      background-color: rgba(245, 34, 45, 0.1);
      border-color: rgba(245, 34, 45, 0.2);
      color: #f5222d;
      
      &.el-tag--dark {
        background-color: #f5222d;
        border-color: #f5222d;
        color: #fff;
      }
      
      &.el-tag--light {
        background-color: rgba(245, 34, 45, 0.08);
        border-color: rgba(245, 34, 45, 0.3);
        color: rgba(245, 34, 45, 0.85);
      }
    }
  }
  
  :deep(.el-button) {
    border-radius: 8px;
    font-weight: 500;
    transition: all 0.3s;
    
    .el-icon {
      margin-right: 4px;
    }
    
    &:hover {
      transform: translateY(-2px);
      box-shadow: 0 4px 12px rgba(0, 0, 0, 0.1);
    }
    
    &.el-button--primary {
      background: linear-gradient(135deg, #1890ff, #36a6ff);
      border-color: #1890ff;
      box-shadow: 0 4px 10px rgba(24, 144, 255, 0.2);
      
      &:hover {
        background: linear-gradient(135deg, #40a9ff, #5cbbff);
        border-color: #40a9ff;
        box-shadow: 0 6px 15px rgba(24, 144, 255, 0.3);
      }
      
      &:active {
        background: linear-gradient(135deg, #0e80ef, #1890ff);
      }
    }
  }

  .pagination-container {
    margin-top: 20px;
    display: flex;
    justify-content: flex-end;
    padding: 10px 0;
    
    :deep(.el-pagination) {
      padding: 0;
      button, li {
        background-color: #f5f7fa;
        border-radius: 4px;
        margin: 0 2px;
        
        &:hover {
          color: #1890ff;
        }
        
        &.is-active {
          background-color: #1890ff;
          color: #fff;
        }
      }
      
      .el-pagination__sizes {
        margin-right: 16px;
      }
    }
  }
  
  :deep(.el-dialog) {
    border-radius: 12px;
    overflow: hidden;
    box-shadow: 0 12px 32px rgba(0, 0, 0, 0.1);
    
    .el-dialog__header {
      padding: 20px 24px;
      border-bottom: 1px solid #f0f0f0;
      margin: 0;
      
      .el-dialog__title {
        font-weight: 600;
      }
    }
    
    .el-dialog__body {
      padding: 24px;
    }
    
    .el-dialog__footer {
      padding: 16px 24px;
      border-top: 1px solid #f0f0f0;
    }
    
    .el-form-item__label {
      font-weight: 600;
      color: #606266;
    }
    
    .el-textarea__inner {
      border-radius: 8px;
      padding: 10px 12px;
      min-height: 120px !important;
      box-shadow: 0 0 0 1px rgba(0, 0, 0, 0.1) inset;
      transition: all 0.3s;
      
      &:hover, &:focus {
        box-shadow: 0 0 0 1px #1890ff inset;
      }
    }
  }
}
</style>
